home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Beziers y otros splines / CanonicalSpline / CanonicalSpline.cs next >
Encoding:
Text File  |  2002-05-08  |  3.6 KB  |  123 lines

  1. //----------------------------------------------
  2. // CanonicalSpline.cs ⌐ 2001 by Charles Petzold
  3. //----------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class CanonicalSpline: Form
  9. {
  10.      protected Point[] apt      = new Point[4];
  11.      protected float   fTension = 0.5f;
  12.  
  13.      public static void Main()
  14.      {
  15.           Application.Run(new CanonicalSpline());
  16.      }
  17.      public CanonicalSpline()
  18.      {
  19.           Text = "Spline can≤nico";
  20.           BackColor = SystemColors.Window;
  21.           ForeColor = SystemColors.WindowText;
  22.           ResizeRedraw = true;
  23.  
  24.           ScrollBar scroll = new VScrollBar();
  25.           scroll.Parent = this;
  26.           scroll.Dock = DockStyle.Right;
  27.           scroll.Minimum = -100;
  28.           scroll.Maximum =  109;
  29.           scroll.SmallChange =  1;
  30.           scroll.LargeChange = 10;
  31.           scroll.Value = (int) (10 * fTension);
  32.           scroll.ValueChanged += new EventHandler(ScrollOnValueChanged);
  33.  
  34.           OnResize(EventArgs.Empty);
  35.      }
  36.      void ScrollOnValueChanged(object obj, EventArgs ea)
  37.      {
  38.           ScrollBar scroll = (ScrollBar) obj;
  39.  
  40.           fTension = scroll.Value / 10f;
  41.  
  42.           Invalidate(false);
  43.      }
  44.      protected override void OnResize(EventArgs ea)
  45.      {
  46.           base.OnResize(ea);
  47.  
  48.           int cx = ClientSize.Width;
  49.           int cy = ClientSize.Height;
  50.  
  51.           apt[0] = new Point(    cx / 4,     cy / 2);
  52.           apt[1] = new Point(    cx / 2,     cy / 4);
  53.           apt[2] = new Point(    cx / 2, 3 * cy / 4);
  54.           apt[3] = new Point(3 * cx / 4,     cy / 2);
  55.      }
  56.      protected override void OnMouseDown(MouseEventArgs mea)
  57.      {
  58.           Point pt;
  59.  
  60.           if (mea.Button == MouseButtons.Left)
  61.           {
  62.                if (ModifierKeys == Keys.Shift)
  63.                     pt = apt[0];
  64.                else if (ModifierKeys == Keys.None)
  65.                     pt = apt[1];
  66.                else
  67.                     return;
  68.           }
  69.           else if (mea.Button == MouseButtons.Right)
  70.           {
  71.                if (ModifierKeys == Keys.None)
  72.                     pt = apt[2];
  73.                else if (ModifierKeys == Keys.Shift)
  74.                     pt = apt[3];
  75.                else
  76.                     return;
  77.           }
  78.           else
  79.                return;
  80.  
  81.           Cursor.Position = PointToScreen(pt);
  82.      }
  83.      protected override void OnMouseMove(MouseEventArgs mea)
  84.      {
  85.           Point pt = new Point(mea.X, mea.Y);
  86.  
  87.           if (mea.Button == MouseButtons.Left)
  88.           {
  89.                if (ModifierKeys == Keys.Shift)
  90.                     apt[0] = pt;
  91.                else if (ModifierKeys == Keys.None)
  92.                     apt[1] = pt;
  93.                else
  94.                     return;
  95.           }
  96.           else if (mea.Button == MouseButtons.Right)
  97.           {
  98.                if (ModifierKeys == Keys.None)
  99.                     apt[2]= pt;
  100.                else if (ModifierKeys == Keys.Shift)
  101.                     apt[3] = pt;
  102.                else
  103.                     return;
  104.           }
  105.           else
  106.                return;
  107.  
  108.           Invalidate();
  109.      }
  110.      protected override void OnPaint(PaintEventArgs pea)
  111.      {
  112.           Graphics grfx  = pea.Graphics;
  113.           Brush    brush = new SolidBrush(ForeColor);
  114.  
  115.           grfx.DrawCurve(new Pen(ForeColor), apt, fTension);
  116.  
  117.           grfx.DrawString("Tensi≤n = " + fTension, Font, brush, 0, 0);
  118.  
  119.           for (int i = 0; i < 4; i++)
  120.                grfx.FillEllipse(brush, apt[i].X - 3, apt[i].Y - 3, 7, 7);
  121.      }
  122. }
  123.